package simpledb;

import java.io.Serializable;
import java.util.Objects;

public class PageLock {
    enum LockType implements Serializable {
        SHARED_LOCK,
        EXCLUSIVE_LOCK;

        @Override
        public String toString() {
            if(this==SHARED_LOCK){
                return "shared lock";
            }
            if (this==EXCLUSIVE_LOCK){
                return "exclusive lock";
            }
            throw new IllegalStateException("impossible to reach here");
        }
    }
    private LockType lockType;
    private TransactionId transactionId;

    public PageLock(LockType lockType, TransactionId transactionId) {
        this.lockType = lockType;
        this.transactionId = transactionId;
    }

    public PageLock(TransactionId transactionId) {
        this.lockType =LockType.SHARED_LOCK;
        this.transactionId = transactionId;
    }

    public LockType getLockType() {
        return lockType;
    }

    public void setLockType(LockType lockType) {
        this.lockType = lockType;
    }

    public TransactionId getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(TransactionId transactionId) {
        this.transactionId = transactionId;
    }

    @Override
    public String toString() {
        return "PageLock{" +
                "lockType=" + lockType +
                ", transactionId=" + transactionId +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PageLock pageLock = (PageLock) o;
        return lockType == pageLock.lockType && Objects.equals(transactionId, pageLock.transactionId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(lockType, transactionId);
    }
}

